package com.cc.service.generate;


import cn.hutool.core.collection.CollectionUtil;
import com.cc.build.BuiderVo;
import com.cc.config.PackagePathConfiguration;
import com.cc.config.TemplateConfiguration;
import com.cc.pojo.bo.TransBo;
import com.cc.pojo.file.BuiderAbstract;
import com.cc.pojo.file.FilePathAndClassName;
import com.cc.pojo.tablestruct.LibaryProperties;
import com.cc.pojo.tablestruct.TableStruct;
import com.cc.service.core.GenerateAbstract;
import com.cc.utils.ConvertHumpUnderlinesToEachOtherUtil;
import com.cc.utils.CoreUtil;
import com.cc.utils.beanUtil.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author c.c
 * @Description 核心生成类
 * @since 2023/3/30
 */

public class GenerateVo extends GenerateAbstract {
  private PackagePathConfiguration packagePathConfiguration = BeanUtils.getBean(PackagePathConfiguration.class);
  private TemplateConfiguration templateConfiguration = BeanUtils.getBean(TemplateConfiguration.class);

  public GenerateVo() {
    this.getTargetPackageAddress();
  }

  @Override
  public List<FilePathAndClassName<BuiderAbstract>> getTargetPackageAddress(Object... youParameter) {
    List<LibaryProperties> libaryProperties = super.getLibaryProperties();
    List<FilePathAndClassName<BuiderAbstract>> packageAddress = new ArrayList<>();
    libaryProperties.forEach(libaryProperty -> {
      if (CollectionUtil.isNotEmpty(libaryProperty.getTableStructs())) {
        StringBuilder packgePath = new StringBuilder();
        //获取输出路径
        CoreUtil.getPath(libaryProperty, packgePath, packagePathConfiguration.isVoisLastLayer(), packagePathConfiguration.getVoPath());
        //获取类名
        String classname = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true);
        String classnameVo = classname + "Vo";
        String classnameDo = classname + "Do";
        String classnameDb = classname + "Db";
        List<String> importPath = Arrays.asList(super.getPathAddress(classnameDo), super.getPathAddress(classnameDb));

        //获取生成类
        List<TransBo> transBos = libaryProperty.getTableStructs()
          .stream()
          .map(TableStruct::convertBuiderDbDtoByTableStruct)
          .collect(Collectors.toList());
        //
        BuiderVo buiderVo = new BuiderVo(packgePath.toString(), importPath, classnameVo, classnameDo, classnameDb, transBos);

        super.putPathAddress(classnameVo, packgePath);
        //转换路径并封装为一个类
        FilePathAndClassName<BuiderAbstract> filePathAndClassName = new FilePathAndClassName<>(packgePath.toString(), classnameVo, buiderVo);
        packageAddress.add(filePathAndClassName);
      }
    });
    super.build(packageAddress, templateConfiguration.getVoPath());
    return packageAddress;
  }
}
